package com.wskj.shiro.chapter3;

import org.apache.shiro.authz.UnauthorizedException;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

/**
 * 
 * @author Administrator
 *
 */
public class PermissionTest extends BaseTest {
	
	
	/**
	 * isPermitted:isPermitted和isPermittedAll用于判断用户是否拥有某个权限或所有权限
	 *   有权限就返回true 没有权限就返回false
	 * 
	 */
	@Ignore
	public void testIsPermitted() {  
	    login("classpath:role/shiro-permission.ini", "zhang", "123");  
	    //login("classpath:role/shiro-permission.ini", "wang", "123");  
	    //判断拥有权限：user:create  
	    Assert.assertTrue(subject().isPermitted("user:create"));  
	    //判断拥有权限：user:update and user:delete  
	    Assert.assertTrue(subject().isPermittedAll("user:update", "user:delete"));  
	    //判断没有权限：user:view  
	    Assert.assertFalse(subject().isPermitted("user:view"));  
	}  
	
	
	/**
	 * checkPermissions:
	 *    检查权限 ，是否有权限。
	 */
    @Test(expected = UnauthorizedException.class)  
    public void testCheckPermission () {  
        login("classpath:role/shiro-permission.ini", "zhang", "123");  
         //断言拥有权限：user:create  
        subject().checkPermission("user:create");  
        //断言拥有权限：user:delete and user:update  
        subject().checkPermissions("user:delete", "user:update"); 
        //断言拥有权限：user:view 失败抛出异常  
        subject().checkPermissions("user:view");  
    }   
	

}
